<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../../js/babel.min.js"></script>
    <script src="../../js/react.development.js"></script>
    <script src="../../js/react-dom.development.js"></script>
    <script src="../../js/prop-types.js"></script>
</head>
<body>
    <div id="example1"></div>
    <div id="example2"></div>
    <div id="example3"></div>
</body>
</html>
<script type="text/babel">
//工厂方法形式
function Person(props) {
    return (
        <ul>
            <li>{props.name}</li>
            <li>{props.age}</li>
            <li>{props.sex}</li>
        </ul>
    )
}
//类的形似
class Person1 extends React.Component{
    render(){
        return(
            <ul>
            <li>{this.props.name}</li>
            <li>{this.props.age}</li>
            <li>{this.props.sex}</li>
        </ul>
        )
    }
}
//指定默认值
Person.defaultProps={
    sex:"女"
}
//对props中的属性值进行类型限制和必要性的限制,需要引入prop-types库
Person.propTypes={
    age:PropTypes.number.isRequired, //如果不加限制，则即使age不写也不会报错，但是isRequired就是要求必须写
    name:PropTypes.string //此处没强调必要性
}
const p1={
    name:'Tom',
    age:18,
    sex:'男'
}
const p2={
    name:'Tom2',
    age:30
}
const p3={
    name:'Tom3',
    age:188,
    sex:'男'
}
ReactDOM.render(<Person name={p1.name} age={p1.age} sex={p1.sex}/>,document.getElementById('example1'))
//针对默认值
ReactDOM.render(<Person1 name={p2.name} age={p2.age}/>,document.getElementById('example2'))
//简略写法
ReactDOM.render(<Person {...p3} />,document.getElementById('example3'))

</script>